<?php 
function  lista_representante(){
$con=Conexion();
	$consulta="select id_tipo_representante,nom_tipo_representante from tbltipo_representante";
	$result=pg_query($con,$consulta);
if(!$result){
		echo'<div class="error_ mensajes"><strong>Error</strong><br/>Parece que ha habido un error en la consulta.</div>';
		//echo pg_last_error();
		exit();
	}
	echo "<select name='opt_trepre' style='width:300px'>\n";
	echo "<option value=''> - seleccione tipo representante - </option>";
	while($row=pg_fetch_row($result))
	{
echo "<option value='$row[0]'> $row[1] </option> ";	
	}
	echo "</select>";
}


function  fechafin($ip){
$pf=date("Y/n/j",strtotime("$ip + 3 year"));
return $pf;
}

function repre($aso) {
	$con=Conexion();
	$consulta="select a.id_asociacion, c.id_cooperativa, p.id_persona, r.id_representante, t.id_tipo_representante ,p.nom_persona, p.apellidos_persona,
	t.nom_tipo_representante, cr.cargo_representante, r.gestion_ini, r.gestion_fin, p.dui_persona 
	from tblasociacion a, tblcooperativa c, tblpersona p, tblrepresentantes r, tbltipo_representante t, tblcargo_representante cr 
	where p.id_persona=r.id_persona and a.id_asociacion=c.id_asociacion and c.id_cooperativa=r.id_cooperativa and r.id_tipo_representante=t.id_tipo_representante 
	and r.id_cargo_representantes=cr.id_cargo_representantes and r.gestion_ini<=current_date and r.gestion_fin>=current_date and a.id_asociacion='$aso' and tipo_eleccion= 'asamblea de constitucion' order by r.id_representante";
	$res = pg_query($con,$consulta);
	echo '<div style="text-align: left; font-weight: bold; color: #1A9AF0;">Integrantes del Consejo de Administraci&oacute;n</div>';
	echo '<table class="tabla-form" border="0">';
	echo '<tr> <td colspan="3"></td> <td> </td><td> </td>
				</tr>';
	//	echo '</table><table class="tabla-form" border="0">';
	while ($row = pg_fetch_row($res)){
		if ($row[4]=='rca0001'){
		echo "<tr> <td>$row[8]</td> <td> $row[5] $row[6]</td> <td>$row[11]</td><td> </td></tr>";
		}
	}
	echo "</table>";
	$res = pg_query($con,$consulta);
	echo ' <div style="text-align: left; font-weight: bold; color: #1A9AF0;">Integrantes de Junta de Vigilancia</div>';
	echo '<table class="tabla-form" border="0">';
	echo '<tr> <td></td><td> </td><td> </td></tr>';
	//echo '</table><table class="tabla-form" border="0">';

	while ($row = pg_fetch_row($res)){
		if ($row[4]=='rjv0001'){
		echo "<tr> <td>$row[8]</td> <td> $row[5] $row[6]</td> <td>$row[11]</td></tr>";
		}
	}
	echo "</table>";
}


function registro($origen,$id) {
	$link=Conexion();
	$consc="select  id_tipo_representante,nom_tipo_representante from tbltipo_representante";
	$result=pg_query($link,$consc);
	if(!$result){
		echo'<div class="error_ mensajes"><strong>Error</strong><br/>Parece que ha habido un error en la consulta.</div>';
		//echo pg_last_error();
		exit();
	}
	echo '<form action="Form_RegRepre.php" method="post" name="formulario" class="validate">
                <table class="tabla-form" border="0">
                            <tr>
                                <td colspan="2"><div class="separador">Ingrese el nombre de la asociacion Coop</div></td>
                                <td width="30"><a href="#" data-tooltip="sticky1"><div class="help"></div></a></td>
                            </tr>
                             <tr>
                                <td width="156"><label>Nombre Cooperativa</label></td>
                               <td><input name="nom_coop" id="nom_coop"  size="80"  class="blur"></input> </td>
                                <td width="30">&nbsp;</td>
                            </tr>
							<tr>
                                <td width="156"><label>Tipo Representantes</label></td><td>';
		
		echo "<select name='opt_rep' id='opt_rep' style='width:208px'>\n";
	echo "<option value=''> - Tipos de Representantes - </option>";
	while($row=pg_fetch_row($result))
	{
	echo '<option value='.$row[0].'> '.$row[1].' </option> ';
	}
	echo "</select>";
		echo '</td><td width="30">&nbsp;</td></tr></table> ';
		$origen='1';	
		echo'<input name="origen" type="hidden" value="'.$origen.'" />';
		echo'<input name="id" type="hidden" value="'. $id.'" />';
		
		/*echo '<div align="right" style="padding:6px;"> <br /><br />
                  <button type="submit" class="boton-form boton-form-link " id="Guardar"  name="Guardar"><img src="../../../Imagenes/Health.png" /> Registrar Representante</button>
                  <button type="button" class="boton-form-x boton-form-link " onClick="window.location.href="../../Usuarios/Vista/Principal.php";"><img src="../../../Imagenes/Cancel.png" /> Cancelar</button>	
                  </div></form> ';*/
		
}

function reg_rep($id_coop,$trep) {
	$link=Conexion();
	echo'
	<tr><td width="104"><label>Fecha de Eleccion</label></td>
	<td width="206"><input readonly type="text" name="txtFechaEleccion" id="datepicker" class="inp" /><div align="left" class="asterisco">
	<span style="float:left;"><img src="../../../Imagenes/calendar.png" data-tooltip="sticky3"/></span></td>
	<strong>&nbsp;*</strong></div>
							
	<td width="162"><label>Inicio de Periodo</label></td>
	<td width="226"><input readonly type="text" name="txtFechaIP" id="date" class="inp" /><div align="left" class="asterisco">
	<span style="float:left;"><img src="../../../Imagenes/calendar.png" data-tooltip="sticky4"/></span></td>
	<strong>&nbsp;*</strong></div>
	
	';
	echo "<input name='opt_rep' type='hidden' value='$trep' />";
	switch ($trep) {
		case 'rel0001': rel();
		;
		break;
		case 'rca0001': rca();
		;
		break;
		case 'rjv0001':rjv();
		;
		break;
		case 'rcl0001':rcl();
		;
		break;
	}
}


function rel() {
	echo '<table class="tabla-form" border="0">';
	echo '<tr> <td colspan="4"><div style="text-align: left; font-weight: bold; color: #1A9AF0;">Representante Legal
	</div></td> <td> </td><td> </td><td> </td> </tr>';
	echo "<tr align='left' ><td>Cargo</td> <td>Nombre</td> <td>Apellido</td> <td>Dui</td><td>Lugar de exten </td></tr>";
	echo '<tr><td>Representante Legal</td><td><input id="nom8" name="nom8" type="text"></td>	<td><input name="ape8" id="ape8"></td>	<td><input name="dui8" id="dui8" maxlength="9">	</td><td><input id="exten8" name="exten8"></td></tr>';
	echo'<input name="cant" type="hidden" value="8" />';
	echo "</table>";
	/*echo '<div align="right" style="padding: 6px;">
				<br /> <span class="mensajes" style="color: #1A9AF0;">Los campos con
					asterisco, son obligatorios</span><br />
				<button type="submit" name="Guardar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Save.png" /> Aceptar
				</button>
				<button type="reset" name="Limpiar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Limpia.png" /> Limpiar
				</button>
				<button type="button" name="Cancelar"
					class="boton-form-x boton-form-link ">
					<img src="../../../Imagenes/Cancel.png" /> Cancelar
				</button>
			</div>';*/
		
}

function rca() {
	echo '<table class="tabla-form" border="0">';
	echo '<tr> <td colspan="4"><div style="text-align: left; font-weight: bold; color: #1A9AF0;">Representante Consejo de Administracion
	</div></td> <td> </td><td> </td><td> </td> </tr>';
	echo '<tr align="left"><td>Cargo</td> 			<td>Nombre</td> 	<td>Apellido</td>	<td>Dui</td>				<td>Lugar de exten </td></tr>';
	echo '<tr><td>Presidente</td>		<td><input id="nom1" name="nom1" type="text"></td>	<td><input name="ape1" id="ape1"></td>	<td><input name="dui1" id="dui1" maxlength="9">	</td><td><input id="exten1" name="exten1"></td></tr>';
	echo '<tr><td>Vice Presidente</td>	<td><input id="nom2" name="nom2" type="text"></td>	<td><input name="ape2" id="ape2"></td>	<td><input name="dui2" id="dui2" maxlength="9">	</td><td><input id="exten2" name="exten2"></td></tr>';
	echo '<tr><td>Secretario</td>		<td><input id="nom3" name="nom3" type="text"></td>	<td><input name="ape3" id="ape3"></td>	<td><input name="dui3" id="dui3" maxlength="9">	</td><td><input id="exten3" name="exten3"></td></tr>';
	echo '<tr><td>Vocal</td>			<td><input id="nom4" name="nom4" type="text"></td>	<td><input name="ape4" id="ape4"></td>	<td><input name="dui4" id="dui4" maxlength="9">	</td><td><input id="exten4" name="exten4"></td></tr>';
	echo '<tr><td>Primer Suplente</td>	<td><input id="nom5" name="nom5" type="text"></td>	<td><input name="ape5" id="ape5"></td>	<td><input name="dui5" id="dui5" maxlength="9">	</td><td><input id="exten5" name="exten5"></td></tr>';
	echo '<tr><td>Segundo Suplente</td>	<td><input id="nom6" name="nom6" type="text"></td>	<td><input name="ape6" id="ape6"></td>	<td><input name="dui6" id="dui6" maxlength="9">	</td><td><input id="exten6" name="exten6"></td></tr>';
	echo '<tr><td>Tercer Suplente</td>	<td><input id="nom7" name="nom7" type="text"></td>	<td><input name="ape7" id="ape7"></td>	<td><input name="dui7" id="dui7" maxlength="9">	</td><td><input id="exten7" name="exten7"></td></tr>';
	echo '<input name="cant" type="hidden" value="7" />';
	//echo "<input name='opt_rep' type='hidden' value='$trep' />";
	echo '</table>';
	/*echo '<div align="right" style="padding: 6px;">
				<br /> <span class="mensajes" style="color: #1A9AF0;">Los campos con
					asterisco, son obligatorios</span><br />
				<button type="submit" name="Guardar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Save.png" /> Registrar Representantes
				</button>
				<button type="reset" name="Limpiar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Limpia.png" /> Limpiar
				</button>
				<button type="button" name="Cancelar"
					class="boton-form-x boton-form-link ">
					<img src="../../../Imagenes/Cancel.png" /> Cancelar
				</button>
			</div>';*/
}

function rjv() {
	echo '<table class="tabla-form" border="0">';
	echo '<tr> <td colspan="4"><div style="text-align: left; font-weight: bold; color: #1A9AF0;">Representante Junta de Vigilancia
	</div></td> <td> </td><td> </td><td> </td> </tr>';
	echo "<tr align='left' ><td>Cargo</td> <td>Nombre</td> <td>Apellido</td> <td>Dui</td><td>Lugar de exten </td></tr>";					
	echo '<tr><td>Presidente</td>		<td><input id="nom1" name="nom1" type="text"></td>	<td><input name="ape1" id="ape1"></td>	<td><input name="dui1" id="dui1" maxlength="9">	</td><td><input id="exten1" name="exten1"></td></tr>';
	echo '<tr><td>Secretario</td>		<td><input id="nom3" name="nom3" type="text"></td>	<td><input name="ape3" id="ape3"></td>	<td><input name="dui3" id="dui3" maxlength="9">	</td><td><input id="exten3" name="exten3"></td></tr>';
	echo '<tr><td>Vocal</td>			<td><input id="nom4" name="nom4" type="text"></td>	<td><input name="ape4" id="ape4"></td>	<td><input name="dui4" id="dui4" maxlength="9">	</td><td><input id="exten4" name="exten4"></td></tr>';
	echo '<tr><td>Primer Suplente</td>	<td><input id="nom5" name="nom5" type="text"></td>	<td><input name="ape5" id="ape5"></td>	<td><input name="dui5" id="dui5" maxlength="9">	</td><td><input id="exten5" name="exten5"></td></tr>';
	echo '<tr><td>Segundo Suplente</td>	<td><input id="nom6" name="nom6" type="text"></td>	<td><input name="ape6" id="ape6"></td>	<td><input name="dui6" id="dui6" maxlength="9">	</td><td><input id="exten6" name="exten6"></td></tr>';
	echo'<input name="cant" type="hidden" value="6" />';
	//echo'<input name="opt_rep" type="hidden" value="$trep" />';
	echo"</table>";
	/*echo '<div align="right" style="padding: 6px;">
				<br /> <span class="mensajes" style="color: #1A9AF0;">Los campos con
					asterisco, son obligatorios</span><br />
				<button type="submit" name="Guardar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Save.png" /> Registrar Representantes
				</button>
				<button type="reset" name="Limpiar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Limpia.png" /> Limpiar
				</button>
				<button type="button" name="Cancelar"
					class="boton-form-x boton-form-link ">
					<img src="../../../Imagenes/Cancel.png" /> Cancelar
				</button>
			</div>';*/
}

function rcl() {
	echo '<table class="tabla-form" border="0">';	echo '<tr> <td colspan="4"><div style="text-align: left; font-weight: bold; color: #1A9AF0;">Representante Comision de Liquidacion
	</div></td> <td> </td><td> </td><td> </td> </tr>';
	echo "<tr align='left' ><td align='left'>Cargo</td> <td>Nombre</td> <td>Apellido</td> <td>Dui</td><td>Lugar de exten </td></tr>";
	echo '<tr><td>Presidente</td>		<td><input id="nom1" name="nom1" type="text"></td>	<td><input name="ape1" id="ape1"></td>	<td><input name="dui1" id="dui1" maxlength="9">	</td><td><input id="exten1" name="exten1"></td></tr>';
	echo '<tr><td>Vice Presidente</td>	<td><input id="nom2" name="nom2" type="text"></td>	<td><input name="ape2" id="ape2"></td>	<td><input name="dui2" id="dui2" maxlength="9">	</td><td><input id="exten2" name="exten2"></td></tr>';
	echo '<tr><td>Secretario</td>		<td><input id="nom3" name="nom3" type="text"></td>	<td><input name="ape3" id="ape3"></td>	<td><input name="dui3" id="dui3" maxlength="9">	</td><td><input id="exten3" name="exten3"></td></tr>';
	echo'<input name="cant" type="hidden" value="3" />';
	//echo'<input name="opt_rep" type="hidden" value="$trep" />';
	echo"</table>";
	/*echo '<div align="right" style="padding: 6px;">
				<br /> <span class="mensajes" style="color: #1A9AF0;">Los campos con
					asterisco, son obligatorios</span><br />
				<button type="submit" name="Guardar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Save.png" /> Registrar Representantes
				</button>
				<button type="reset" name="Limpiar"
					class="boton-form boton-form-link submit">
					<img src="../../../Imagenes/Limpia.png" /> Limpiar
				</button>
				<button type="button" name="Cancelar"
					class="boton-form-x boton-form-link ">
					<img src="../../../Imagenes/Cancel.png" /> Cancelar
				</button>
			</div>';*/
}




function representante($nom,$ape,$dui,$exten,$i,$trep,$pi,$pf,$fe,$coop){
	$link=Conexion();
	$mun=CodMunicipio($exten);
	$c="select spcrea_persona('$nom','$ape','$dui','$mun')";
	$res=pg_query($link,$c);
	$persona=pg_fetch_row($res);
	switch ($i) {
		case '1'://presidente
			$r="select spcrea_repre('$persona[0]','$trep','crp0001','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "presidente: $repre[0]";
		;
		break;
		case '2'://vice_pre
			$r="select spcrea_repre('$persona[0]','$trep','crp0002','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "vice-presidente: $repre[0]";
		;
		break;
		case '3'://secretaria
			$r="select spcrea_repre('$persona[0]','$trep','crp0003','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "secretaria: $repre[0]";
			echo " ";
		;
		break;
		case '4'://vocal
			$r="select spcrea_repre('$persona[0]','$trep','crp0004','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "vocal: $repre[0]";
			echo " ";
		;
		break;
		case '5'://sup1
			$r="select spcrea_repre('$persona[0]','$trep','crp0005','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "supplente 1: $repre[0]";
			echo " ";
		;
		break;
		case '6'://sup2
			$r="select spcrea_repre('$persona[0]','$trep','crp0006','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "supplente 2: $repre[0]";
			echo " ";
		;
		break;
		case '7'://sup3
			$r="select spcrea_repre('$persona[0]','$trep','crp0007','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "supplente : $repre[0]";
			echo " ";
		;
		break;
		case '8'://repLegal
			$r="select spcrea_repre('$persona[0]','$trep','crp0008','$pi','$pf','Asamblea','$coop','$fe')";
			$res_crea=pg_query($link,$r);
			$repre=pg_fetch_row($res_crea);
			excepcion($repre[0]);
			echo "Represenatnte Legal: $repre[0]";
			echo " ";
		;
		break;
		
	}
}

function CodMunicipio($mun){
	$link=Conexion();
	$result=pg_query($link,"select cod_municipio from tblmunicipio where nom_municipio='$mun'");
	if(!$result){
		echo'<div class="error_ mensajes"><strong>Error</strong><br/>Parece que ha habido un error en la consulta.</div>';
		//echo pg_last_error();
		exit();
	}
	$cod=pg_fetch_row($result);
	return $cod[0];
}

function  muestra_repre($cop, $trep){
	echo $cop;
	echo $trep;
$con=Conexion();
$consulta="select  p.id_persona, r.id_representante,cr.cargo_representante, p.nom_persona, p.apellidos_persona,r.tipo_eleccion ,r.gestion_ini, r.gestion_fin, r.fecha_eleccion
	from tblasociacion a, tblcooperativa c, tblpersona p, tblrepresentantes r, tbltipo_representante t, tblcargo_representante cr
	where p.id_persona=r.id_persona and a.id_asociacion=c.id_asociacion and c.id_cooperativa=r.id_cooperativa and r.id_tipo_representante=t.id_tipo_representante 
	and r.id_cargo_representantes=cr.id_cargo_representantes and r.gestion_ini<=current_date and r.gestion_fin>=current_date and
	c.id_cooperativa='$cop' and t.id_tipo_representante='$trep' and tipo_eleccion='Asamblea'  order by r.id_representante";

$res = pg_query($con,$consulta);
if(!$res){
		echo'<div class="error_ mensajes"><strong>Error</strong><br/>Parece que ha habido un error en la consulta.</div>';
		echo pg_last_error();
		exit();
	}
	
	$i=1;
	$cols=pg_num_rows($res);
	if ($cols=='0'){
		echo'<div class="error_ mensajes"><strong>Error</strong><br/>Para esta asociacion Cooperativa no existen Representantes registrados.</div>';
	}
	else {
		echo '<table class="class="tabla-general tabla-cebra2 display2" border="0">';
		echo '<thead><tr colspan="7"><td>No</td><td>Cargo</td><td>Nombre Completo</td><td>Eleccion</td><td>Fecha de Eleccion</td><td>Inicio Periodo</td><td>Fin de Periodo</td></tr></thead>';
		while ($row = pg_fetch_row($res)){
		echo "<tr><td>$i</td> <td>$row[2]</td> <td> $row[3] $row[4]</td> <td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td></tr>";
		$i=$i+1;
		
	}
	echo "</table>";
	}//else


}//funcion muestra_repre

function muestra_asientos($cop){
	echo $cop;
	$consulta="select nom_clase_coop, num_libro,num_folio_inicial, paginacion_inicial,num_folio_fin, paginacion_final, art_estatutos 
	from  tblcooperativa natural join tblasiento_registral natural join tbltipo_asiento  natural join tbltipo_coop natural join tblclase_coop 
	where id_cooperativa='$cop' and id_tipo_asiento='tar0001'";
	$con=Conexion();
	$res = pg_query($con,$consulta);
	$row = pg_fetch_row($res);
	
	echo '<table class="tabla-cebra" border="0">
				<thead>
				<tr><td></td><td>b</td><td>c</td><td>d</td><td>e</td><td>f</td><td>g</td></tr>
				</thead>
				<tbody>
				<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
				</tbody>
				</table>';

}//funcion muestra_asiento


?>